/*
 * SimpleFilter.java
 *
 * Created on December 16, 2005, 11:26 AM
 *
 */
package biofilter.filters;

import biofilter.exceptions.InvalidRecordException;
import biofilter.records.Record;
import biofilter.sources.RecordsSourceBase;

/**
 * To provide default implementations of the RecordsFilter patterns in a way that is common 
 * to most simple filters, according to the pipes and filters pull filter pattern. 
 *
 * @author yan, tom, rox
 */
public abstract class SimpleFilter extends RecordsFilter {

    /** Creates a new instance of SimpleFilter */
    public SimpleFilter() {
        super();
    }

    /** Creates a new instance of SimpleFilter and sets the source for the filter.
     * @param RecordsSourceBase
     */
    public SimpleFilter(RecordsSourceBase rs) {
        super(rs);
    }

    /**
     * This method gets a record from the source, passes it to transformRecord()
     * and then returns the transformed record. If trackOriginalRecord is set
     * to true, then the record obtained from the source is saved in the
     * originalRecords LinkedList. If trackTransformedRecord is set to true,
     * then the transformed record is saved in the transformedRecords LinkedList.
     * By default, both of these are set to false.
     * @return Record - the transformed record
     */
    public Record getRecord() throws InvalidRecordException {
        this.transformedRecord = null;

        if (!eof()) {
            this.originalRecord = source.getRecord();

            if (this.originalRecord != null) {
                if (trackOriginalRecords) {
                    this.originalRecords.add(this.originalRecord);
                }

                this.transformedRecord = this.transformRecord(this.originalRecord);

                if (trackTransformedRecords) {
                    this.transformedRecords.add(this.transformedRecord);
                }
            }
        }

        return this.transformedRecord;
    }

    /**
     * To indicate whether all records have been processed and returned by this filter.
     * @return boolean - true if all records have been processed, false if not
     */
    public boolean eof() {
        return source.eof();
    }
}
